package com.qf.fmall.order.service.impl;

import com.qf.fmall.order.entity.OrderItem;
import com.qf.fmall.order.entity.Orders;
import com.qf.fmall.order.mapper.OrderItemMapper;
import com.qf.fmall.order.mapper.OrdersMapper;
import com.qf.fmall.order.service.IOrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.fmall.order.vo.AddOrderResultVo;
import com.qf.fmall.pay.WxPayUtils;
import com.qf.fmall.shopcart.entity.ShoppingCart;
import com.qf.fmall.shopcart.service.IShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 订单  服务实现类
 * </p>
 *
 * @author wbw
 * @since 2023-12-01
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersService {
    @Autowired
    private IShoppingCartService shoppingCartService;
    @Autowired
    private OrderItemMapper orderItemMapper;
    @Autowired
    private OrdersMapper ordersMapper;
    @Override
    public AddOrderResultVo addOrder(Orders orders, Integer[] cids) throws ParseException {
        //1.写入订单主表数据
          //1.1生成订单编号
        String orderId = UUID.randomUUID().toString().replaceAll("-","");
        orders.setOrderId(orderId);
          //1.2处理商品名称
        List<ShoppingCart> carts = shoppingCartService.listbycids(Arrays.asList(cids));
        StringBuffer stringBuffer = new StringBuffer();
        for (ShoppingCart cart : carts) {
            String productName = cart.getProductName();
            stringBuffer.append(productName);
            stringBuffer.append(",");
        }
        orders.setUntitled(stringBuffer.toString());
        //1.3处理订单状态为代付款
        orders.setStatus("1");
          //1.4把订单的逻辑删除状态设置为 未删除
        orders.setDeleteStatus(0);
          //1.5订单创建时间和修改时间
        orders.setCreateTime(new Date());
        orders.setUpdateTime(new Date());
        ordersMapper.insert(orders);
        //2.写入订单明细表数据
        //订单数据表的数据 来源于购物车
        for (ShoppingCart cart : carts) {
            //对于每个购物车对象都要转为一个对应的订单明细对象
            OrderItem orderItem = new OrderItem();
            //2.1生成订单明细表主键
            String itemId = UUID.randomUUID().toString();
            orderItem.setItemId(itemId);
            //2.2设置订单编号
            orderItem.setOrderId(orderId);
            orderItem.setProductId(cart.getProductId()+"");
            orderItem.setProductName(cart.getProductName());
            orderItem.setProductImg(cart.getProductImg());
            orderItem.setSkuId(cart.getSkuId());
            orderItem.setSkuName(cart.getSkuName());
            orderItem.setProductPrice(cart.getProductPrice());
            orderItem.setBuyCounts(Integer.valueOf(cart.getCartNum()));
            //计算商品金额
            BigDecimal multiply = new BigDecimal(cart.getCartNum()).multiply(new BigDecimal(cart.getSellPrice()));

            orderItem.setTotalAmount(multiply);
            String cartTime = cart.getCartTime();

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date cartData = simpleDateFormat.parse(cartTime);
            orderItem.setBasketDate(cartData);
            //保存订单明细对象
            orderItemMapper.insert(orderItem);
        }


        //3.获取微信支付的url
        String payUrl = WxPayUtils.getPayUrl(orders);

        //4.返回AddOrderResultVo

        AddOrderResultVo data = new AddOrderResultVo();
        data.setOrderId(orderId);
        data.setProductNames(orders.getUntitled());
        data.setPayUrl(payUrl);

        //5.把购物车中的对应数据删掉
        shoppingCartService.removeBatchByIds(Arrays.asList(cids));

        return data;
    }
}
